package br.com.mundolivre.mail;

import java.io.UnsupportedEncodingException;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.mail.MessagingException;

import br.com.mundolivre.model.Email;

public class ThreadEmail extends Thread{
	
	private Email email;
	
	public ThreadEmail(Email email){
		this.email = email;
	}
	
	public Email getEmail() {
		return email;
	}



	public void setEmail(Email email) {
		this.email = email;
	}



	public void run(){
		SimpleDateFormat sd = new SimpleDateFormat("dd/MM/yyyy - HH:mm");		
		
		MailJava mj = new MailJava();
		mj.setSmtpHostMail("smtp.gmail.com");
		mj.setSmtpPortMail("587");
		mj.setSmtpAuth("true");
		mj.setSmtpStarttls("true");
		mj.setUserMail("no-reply@mundolivre.com.br");
		mj.setFromNameMail(email.getNome());
		mj.setPassMail("zya492013");
		//zya492013
		mj.setCharsetMail("ISO-8859-1");
		mj.setSubjectMail("Contato - SITE " + sd.format(new Date()));
		mj.setBodyMail(email.formataEmail(email));
		mj.setTypeTextMail(MailJava.TYPE_TEXT_PLAIN);

		Map<String, String> map = new HashMap<String, String>();		
		
		//System.out.println(email.getEmpresa());
		if(email.getEmpresa() != null){
			if(email.getEmpresa().equals("s")){
				map.put("comercial@mundolivre.com.br", "Comercial Mundo Livre");
			}if(email.getEmpresa().equals("t")){
				map.put("maurelio@mundolivre.com.br", "Comercial Mundo Livre");
			}
			if(email.getEmpresa().equals("st")){
				map.put("comercial@mundolivre.com.br", "Comercial Mundo Livre");
				map.put("maurelio@mundolivre.com.br", "Comercial Mundo Livre");
			}
		}else{
			map.put("comercial@mundolivre.com.br", "Comercial Mundo Livre");
			map.put("maurelio@mundolivre.com.br", "Comercial Mundo Livre");
		}
		

		

		mj.setToMailsUsers(map);

		List<String> files = new ArrayList<String>();
		//files.add("C:\\images\\ajax_loader.gif");

		mj.setFileMails(files);

		try {
			new MailJavaSender().senderMail(mj);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
		
	}

}
